package com.weixin.paleituo.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.weixin.paleituo.activity.CheckstandActivity;
import com.weixin.paleituo.activity.OrdersActivity;
import com.weixin.paleituo.activity.PayForSuccessActivity;
import com.weixin.paleituo.activity.PaySuccessActivity;
import com.weixin.paleituo.annotation.SuccessFromType;
import com.weixin.paleituo.annotation.WeChatPayType;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.util.DateTools;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.ToastUtils;

/**
 * Author yichao
 * Time  2018/3/2 15:11
 * Dest  微信支付
 */

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private String Tag="WXPayEntryActivity";
    private   IWXAPI mIWXAPI;
    private String mData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*   req.extData=roomId+"#*#"+mMoneyStr+"#*#"+mOrderBean.getOrderId()+"#*#"+payStyle;

         var1.putString("_wxapi_payreq_appid", this.appId);
        var1.putString("_wxapi_payreq_partnerid", this.partnerId);
        var1.putString("_wxapi_payreq_prepayid", this.prepayId);
        var1.putString("_wxapi_payreq_noncestr", this.nonceStr);
        var1.putString("_wxapi_payreq_timestamp", this.timeStamp);
        var1.putString("_wxapi_payreq_packagevalue", this.packageValue);
        var1.putString("_wxapi_payreq_sign", this.sign);
        var1.putString("_wxapi_payreq_extdata", this.extData);
        var1.putString("_wxapi_payreq_sign_type", this.signType);
*/
        Intent intent = getIntent();
       mData = intent.getStringExtra("_wxapi_payresp_extdata");
        mIWXAPI = WXAPIFactory.createWXAPI(this, WXPayEntry.APP_ID,true);

        mIWXAPI.handleIntent(intent,this);

    }


    @Override
    public void onReq(BaseReq baseReq) {
        LogUtils.i(Tag,baseReq.toString());

    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mIWXAPI.handleIntent(intent, this);
    }

    @Override
    public void onResp(BaseResp resp) {

        LogUtils.i(Tag,resp.toString());
        String[] split = mData.split("--");

        if(resp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
            ToastUtils.makeText("支付返回码"+resp.errCode);
            LogUtils.i(Tag,"支付返回码"+resp.errCode+"");
           LogUtils.i(Tag,"微信回调时间 ：："+ DateTools.getMixedDate());

           switch (split[0]){
               case WeChatPayType.FORM_HOUSE:
                   if (resp.errCode==0){
                       //支付成功
                       PayForSuccessActivity.startActivity(this,split[1]);
                       BaseApplication.finishActivity(OrdersActivity.class);
                       finish();
                   }else if (resp.errCode==-2){
                       //用户取消
                       BaseApplication.finishActivity(OrdersActivity.class);
                       finish();
                   }else if (resp.errCode==-1){
                       //支付失败
                       BaseApplication.finishActivity(OrdersActivity.class);
                       finish();
                   }
                   break;
               case WeChatPayType.FORM_GOODS:
                   if (resp.errCode == 0) {
                       //支付成功
                       ToastUtils.makeText("支付成功");
                       BaseApplication.finishActivity(CheckstandActivity.class);
                       startActivity(PaySuccessActivity.call2SuccessActivity(this, SuccessFromType.PAY));
                       finish();
                   } else if (resp.errCode == -2) {
                       //用户取消
                       ToastUtils.makeText("取消支付");
                       finish();
                   } else if (resp.errCode == -1) {
                       //支付失败
                       ToastUtils.makeText("支付失败");
                       finish();
                   }
                   break;
           }

        }

    }
}
